require 'singleton'                          

require 'rubygems'
require 'sinatra'

#
# This class is a VERY simple list of Tasks
# is just a Hash where each Task can be false (not done)
# or true (done)
#
class Tasks < Hash
  include Singleton
                   
  def initialize
         super
  end
end

#
# This method runs once at startup, we add just a 
# sample 
#
configure do
    Tasks.instance["Buy milk!"]=true
    Tasks.instance["Learn some Ruby"]=false
end
#
# List, send this when accessing 
# http://127.0.0.1:4567/list
# (GET)
#

get '/list' do
    #use the todolist template to generate a html doc
    erb :tasklist
end

#
# Show a new Task form
#
get '/new' do
    erb :taskform
end

#
# Add a new task
#

post '/add' do
    task_name = params['task']

    Tasks.instance[task_name] = false

    redirect '/list'
end


__END__

@@ tasklist
<html>
<head>
<title>My Tasks</title>
</head>

<body>
  <h1>Tasks</h1>
    <ul>
      <% Tasks.instance.each do |key,value| %>
      <% style="text-decoration: line-through" if value %>
      <li style="<%= style %>"><%= key %></li>
      <% end %>
    </ul>
</body>

</html>

@@ taskform

<html>
<head>
<title>New Task</title>
</head>

<body>
  <h1>Add a new Task</h1>
  <form action="/add" method="post">
    <input type="text" name="task"/>
    <input type="submit"/>
  </form>
</body>

</html>
